package com.cowealth.platform.common.web.servlet;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * cors跨域配置
 *
 * PropertySource.ignoreResourceNotFound:如果指定的属性源不存在，是否要忽略这个错误
 *
 * @author : skycdm
 * @version : 1.0.0
 * @date : 2021/2/24 14:06
 */
@Component
@ConfigurationProperties(prefix = "spring.web.cors")
@PropertySource(
        name = "cors.properties",
        value = {"classpath:cors.properties"},
        ignoreResourceNotFound = true,
        encoding = "UTF-8" )
public class CorsConfig {

    private List<String> allowedOrigins = new ArrayList();

    private List<String> allowedMethods = new ArrayList();

    private List<String> allowedHeaders = new ArrayList();

    private List<String> exposedHeaders = new ArrayList();

    private Boolean allowCredentials;

    @DurationUnit(ChronoUnit.SECONDS)
    private Duration maxAge = Duration.ofSeconds(1800L);


    /**
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //对接口配置跨域设置
        source.registerCorsConfiguration("/**", buildCorsConfiguration());
        CorsFilter corsFilter = new CorsFilter(source);
        //通过FilterRegistrationBean实例设置优先级可以生效
        FilterRegistrationBean<CorsFilter> corsBean = new FilterRegistrationBean<>(corsFilter);
        //设置优先级，越低越优先
        corsBean.setOrder(1);
        return corsBean;
    }


    private CorsConfiguration buildCorsConfiguration() {
        //如果没有配置，默认开启禁止跨域
        if (CollectionUtils.isEmpty(this.allowedOrigins)) {
            return null;
        } else {
            PropertyMapper map = PropertyMapper.get();
            CorsConfiguration configuration = new CorsConfiguration();
            map.from(this::getAllowedOrigins).to(configuration::setAllowedOrigins);
            map.from(this::getAllowedHeaders).whenNot(CollectionUtils::isEmpty).to(configuration::setAllowedHeaders);
            map.from(this::getAllowedMethods).whenNot(CollectionUtils::isEmpty).to(configuration::setAllowedMethods);
            map.from(this::getExposedHeaders).whenNot(CollectionUtils::isEmpty).to(configuration::setExposedHeaders);
            map.from(this::getMaxAge).whenNonNull().as(Duration::getSeconds).to(configuration::setMaxAge);
            map.from(this::getAllowCredentials).whenNonNull().to(configuration::setAllowCredentials);
            return configuration;
        }
    }


    public List<String> getAllowedOrigins() {
        return allowedOrigins;
    }

    public void setAllowedOrigins(List<String> allowedOrigins) {
        this.allowedOrigins = allowedOrigins;
    }

    public List<String> getAllowedMethods() {
        return allowedMethods;
    }

    public void setAllowedMethods(List<String> allowedMethods) {
        this.allowedMethods = allowedMethods;
    }

    public List<String> getAllowedHeaders() {
        return allowedHeaders;
    }

    public void setAllowedHeaders(List<String> allowedHeaders) {
        this.allowedHeaders = allowedHeaders;
    }

    public List<String> getExposedHeaders() {
        return exposedHeaders;
    }

    public void setExposedHeaders(List<String> exposedHeaders) {
        this.exposedHeaders = exposedHeaders;
    }

    public Boolean getAllowCredentials() {
        return allowCredentials;
    }

    public void setAllowCredentials(Boolean allowCredentials) {
        this.allowCredentials = allowCredentials;
    }

    public Duration getMaxAge() {
        return maxAge;
    }

    public void setMaxAge(Duration maxAge) {
        this.maxAge = maxAge;
    }
}
